#
# @lc app=leetcode.cn id=2389 lang=python3
#
# [2389] 和有限的最长子序列
#

# @lc code=start
from itertools import accumulate
from typing import List


class Solution:
    def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        nums.sort()
        s = list(accumulate(nums,initial=0))
        def find(x : int) -> int:
            l = 0
            r = len(s)-1
            while l < r:
                mid = l + (r-l+1)//2
                if s[mid] <= x:
                    l = mid
                else:
                    r = mid-1
            return l
        
        return [find(q) for q in queries]
# @lc code=end

